<?php

namespace App\Http\ViewComposers;

use App\Handlers\role;
use Illuminate\View\View;
use App\Services\Config\ChannelsService;
use Illuminate\Support\Facades\Auth;

class ChannelAllComposer
{
    protected $channelService;

    public function __construct(ChannelsService $channelsService)
    {
        $this->channelsService = $channelsService;
    }

    public function compose(View $view)
    {
        $defaultCheck = $view->defaultCheck;
        $reqGameId = empty($view->reqGameId) ? old('game_id') : $view->reqGameId;
        $reqChannelTableId = empty($view->reqChannelTableId) ? old('channel_table_id') : $view->reqChannelTableId;
        $channels = $this->channelsService->getAll();

        //拼出 渠道table
        $channelHtml = '';
        foreach ($channels as $channel) {
            $checked = '';
            $show = 'display:none;';
            $gameId = $channel["game_id"];
            $channelTableId = $channel["id"];
            $channelName = "{$channel['game']['game_name']}({$channel['channel_name']})";

            if ((empty($reqChannelTableId) && $defaultCheck !== false)
                || (isset($reqChannelTableId) && in_array($channelTableId, $reqChannelTableId))) {
                $checked = 'checked="checked"';
            }

            if (isset($reqGameId) && in_array($gameId, $reqGameId)) {
                $show = '';
            }

            $channelHtml .= <<<EOF
<div class="col-sm-1" style="min-width: 20rem;$show" data-game-id="$gameId">
    <label>&emsp;$channelName
            <input type="checkbox" class="pull-left" $checked name="channel_table_id[]" value="$channelTableId">
    </label>
</div>
EOF;
        }


        $view->with(compact('channelHtml'));
    }
}